{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# DAY2 字符串与比较运算\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 一、变量的类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.1 认识变量类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name 的类型: <class 'str'>\n",
      "age 的类型: <class 'int'>\n",
      "height 的类型: <class 'float'>\n",
      "is_student 的类型: <class 'bool'>\n"
     ]
    }
   ],
   "source": [
    "# Python 中常见的基本数据类型\n",
    "name = \"小明\"          # 字符串 (str)\n",
    "age = 18              # 整数 (int)\n",
    "height = 1.75         # 浮点数 (float)\n",
    "is_student = True     # 布尔值 (bool)\n",
    "\n",
    "# 使用 type() 函数查看变量类型\n",
    "print(f\"name 的类型: {type(name)}\")\n",
    "print(f\"age 的类型: {type(age)}\")\n",
    "print(f\"height 的类型: {type(height)}\")\n",
    "print(f\"is_student 的类型: {type(is_student)}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们再来看一下vscode的变量功能"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.2 类型转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "年龄是: 18\n",
      "分数是: 95\n",
      "价格是: 19.9\n"
     ]
    }
   ],
   "source": [
    "# 有时需要在不同类型之间转换\n",
    "age = 18\n",
    "age_str = str(age)           # 转换为字符串\n",
    "print(f\"年龄是: {age_str}\")\n",
    "\n",
    "score = \"95\"\n",
    "score_num = int(score)       # 转换为整数\n",
    "print(f\"分数是: {score_num}\")\n",
    "\n",
    "price = \"19.9\"\n",
    "price_float = float(price)   # 转换为浮点数\n",
    "print(f\"价格是: {price_float}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 二、字符串操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.1 字符串的基本操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "拼接结果: Hello Python\n",
      "字符串长度: 12\n"
     ]
    }
   ],
   "source": [
    "str1 = \"Hello\"\n",
    "str2 = \"Python\"\n",
    "\n",
    "# 1. 字符串拼接\n",
    "greeting = str1 + \" \" + str2\n",
    "print(f\"拼接结果: {greeting}\")\n",
    "\n",
    "# 2. 计算字符串长度\n",
    "length = len(greeting)\n",
    "print(f\"字符串长度: {length}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.2 字符串的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个字符: H\n",
      "第二个字符: e\n",
      "最后一个字符: n\n"
     ]
    }
   ],
   "source": [
    "# 3. 获取指定位置的字符\n",
    "first_char = greeting[0]      # 第一个字符\n",
    "second_char = greeting[1]     # 第二个字符\n",
    "last_char = greeting[-1]      # 最后一个字符\n",
    "\n",
    "print(f\"第一个字符: {first_char}\")\n",
    "print(f\"第二个字符: {second_char}\")\n",
    "print(f\"最后一个字符: {last_char}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.3 字符串的切片"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "核心口诀：取左不取右"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "前 5 个字符: Hello\n",
      "第 6-11 个字符: Python\n",
      "前 5 个字符: Hello\n",
      "从第 6 个字符到末尾: Python\n",
      "完整字符串: HelloPython\n"
     ]
    }
   ],
   "source": [
    "text = \"HelloPython\"\n",
    "\n",
    "# 切片语法: 字符串[起始位置:结束位置]\n",
    "# 注意: 包含起始位置,不包含结束位置(取左不取右)\n",
    "\n",
    "# H e l l o P y t h o n\n",
    "# 0 1 2 3 4 5 6 7 8 9 10\n",
    "\n",
    "print(f\"前 5 个字符: {text[0:5]}\")      # Hello (索引0到4)\n",
    "print(f\"第 6-11 个字符: {text[5:11]}\")  # Python (索引5到10)\n",
    "\n",
    "# 省略起始位置,从开头开始\n",
    "print(f\"前 5 个字符: {text[:5]}\")       # Hello\n",
    "\n",
    "# 省略结束位置,到末尾结束  \n",
    "print(f\"从第 6 个字符到末尾: {text[5:]}\")  # Python\n",
    "\n",
    "# 同时省略,复制整个字符串\n",
    "print(f\"完整字符串: {text[:]}\")         # HelloPython"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 三、比较运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "75 > 90: False\n",
      "75 <= 90: True\n",
      "75 == 90: False\n",
      "75 != 90: True\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 比较运算符用于比较两个值,结果是布尔值(True 或 False)\n",
    "\n",
    "score_a = 75\n",
    "score_b = 90\n",
    "\n",
    "# 常用比较运算符:\n",
    "# >  大于\n",
    "# <  小于\n",
    "# >= 大于等于\n",
    "# <= 小于等于\n",
    "# == 等于\n",
    "# != 不等于\n",
    "\n",
    "is_a_higher = score_a > score_b              # False\n",
    "is_a_lower_or_equal = score_a <= score_b     # True\n",
    "is_equal = score_a == score_b                # False\n",
    "is_different = score_a != score_b            # True\n",
    "\n",
    "print(f\"{score_a} > {score_b}: {is_a_higher}\")\n",
    "print(f\"{score_a} <= {score_b}: {is_a_lower_or_equal}\")\n",
    "print(f\"{score_a} == {score_b}: {is_equal}\")\n",
    "print(f\"{score_a} != {score_b}: {is_different}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "vs",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
